<template>
  <div class="app-container">
    <div class="add-btn">
      <el-button type="primary" @click="getAddUser()">添加用户</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="用户名称" width="250">
        <template slot-scope="scope">{{ scope.row.userName }}</template>
      </el-table-column>
      <el-table-column label="入库管理" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.inWarehouse" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="出库管理" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.outWarehouse" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="物资处理" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.goods" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="查询统计" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.statistics" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="基础信息" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.basic" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="系统管理" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.system" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getUserlist, deleteUser } from "@/api/user";

export default {
  data() {
    return {
      instorageValue: true,
      tableData: []
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    getAddUser() {
      this.$router.push({ path: "/manage/addUser" })
    },
    async getUserList() {
      let res = await getUserlist({ current: "0", size: "5" });
      this.tableData = res.data.data.items;
      this.tableData.forEach(item => {
        for (let key in item) {
          if (item[key] == "1" && key != 'myId') {
            item[key] = true;
          } else if (item[key] == "0") {
            item[key] = false;
          }
        }
      });
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.$router.push({
        name: "EditUser",
        params: {
          userInfo: row
        }
      })
      sessionStorage.setItem("USER_INFO", JSON.stringify(row))
    },
    async handleDelete(index, row) {
      let res = await deleteUser({myId: row.myId})
      console.log(index, row.myId);
      console.log(res);
      this.getUserList()
    }
  }
};
</script>

<style>
.add-btn {
  display: flex;
  justify-content: end;
}
</style>
